import 'package:flutter/cupertino.dart';
import 'package:shared_preferences/shared_preferences.dart';

///测试状态管理
class IsFollow with ChangeNotifier {
  ///热门搜索
  List <String> recent_search = [];

  IsFollow();

  ///插入最近词汇
  setRecentSearch(String keyWord) async {
   await readData();
     recent_search.removeWhere((element) => element == keyWord);
     if (recent_search.length >= 10) {
       recent_search.removeLast();
     }
     recent_search.insert(0, keyWord);
     print("---------------$recent_search:数据--------------------");
     saveData();
     notifyListeners();
  }

  ///保存数据到本地
  saveData() async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setStringList('search_latelywords', recent_search);
  }


  ///获取本地数据
  Future<List<String>> readData() async {
    var prefs = await SharedPreferences.getInstance();
    List<String> recentList= prefs.getStringList('search_latelywords');
    this.recent_search = recentList;
    notifyListeners();
  }

  ///清除热门词汇
  cleanRecentSearch() {
    recent_search.clear();
    notifyListeners();
  }
}